package com.hqbzl.c1;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

@Slf4j
public class TestSleepAndNotify5 {
    static final Object room = new Object();
    static boolean result = false;

    public static void sleep(long t) {
        try {
            TimeUnit.SECONDS.sleep(t);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (room) {
                log.debug("等待结果");
                while (!result) {
                    try {
                        room.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                log.debug("获取结果 result: {}", result);
            }
        }, "等结束线程").start();
        new Thread(() -> {
            synchronized (room) {
                // 模拟计算了5秒
                log.debug("开始计算");
                sleep(5);
                log.debug("结束计算");
                result = true;
                // 错误唤醒
//              room.notify();
                // 错误唤醒
                room.notifyAll();
            }
        }, "calc线程").start();
    }
}
